<template>
  <div>
    <Row :gutter="16">
      <Col span="24">
        <Card dis-hover class="pandx">
          <Row style="margin-bottom:10px;">
            <Col span="24" style="text-align: right;">
              <Button :disabled="!hasPermission('Pages.Administration.Settings.Save')" icon="md-construct"
                      type="primary" @click="save">保存全部设置
              </Button>
            </Col>
          </Row>
          <Form ref="form" :model="setting" :rules="rules" label-postion="left">
            <Tabs>
              <TabPane label="账户安全" name="security">
                <Card dis-hover>
                  <Checkbox v-model="setting.security.userLockout.isEnabled">
                    登录失败后启用账户锁定
                  </Checkbox>
                  <collapse-transition>
                    <div style="margin-top:20px;margin-left:20px;">
                      <template v-if="setting.security.userLockout.isEnabled">

                        <div style="margin-bottom: 10px;">
                          在账户被锁定之前最多尝试登录
                          <InputNumber
                              v-model="setting.security.userLockout.maxFailedAccessAttemptsBeforeLockout"
                              :editable="false" :max="10" :min="0"
                              style="width:80px;"></InputNumber>
                          次
                        </div>

                        <div style="margin-bottom: 20px;">
                          账户锁定
                          <InputNumber
                              v-model="setting.security.userLockout.defaultAccountLockoutSeconds"
                              :editable="false" :max="18000" :min="0" :step="10"
                              style="width:80px;"></InputNumber>
                          秒
                        </div>
                      </template>
                    </div>
                  </collapse-transition>
                  <Checkbox v-model="setting.security.allowOneConcurrentLoginPerUser">
                    相同用户只能在一个地方登录
                  </Checkbox>
                </Card>
              </TabPane>
              <TabPane label="审计日志" name="audit">
                <Card dis-hover>
                  只保留
                  <InputNumber
                      v-model="setting.periodicBackgroundWorker.expireAuditLogDelete"
                      :editable="false" :max="730" :min="0" :step="1"
                      style="width:80px;"></InputNumber>
                  天的审计日志
                </Card>
              </TabPane>
              <TabPane label="邮件（SMTP）" name="mail">
                <Card dis-hover>
                  该版本无此功能
                </Card>
              </TabPane>
            </Tabs>
          </Form>
        </Card>
      </Col>
      <Col span="12">

      </Col>
    </Row>

  </div>
</template>

<script lang="ts">
import {Component, Vue, Prop, Watch, Emit} from 'vue-property-decorator';
import AbpBase from '@/lib/abpbase';
import Util from '@/lib/util';
import CollapseTransition from '@/components/common/collapse-transition';

interface HostSetting {
  email:{
    defaultFromAddress:string,
    defaultFromDisplayName:string,
    smtpDomain:string,
    smtpEnableSsl:boolean,
    smtpHost:string,
    smtpPassword:string,
    smtpPort:number,
    smtpUseDefaultCredentials:boolean,
    smtpUserName:string,
  },
  periodicBackgroundWorker:number,
  security: {
    allowOneConcurrentLoginPerUser:boolean,
    userDefaultPasswordComplexitySettings:boolean,
    userLockout: {
      isEnabled: boolean,
      maxFailedAccessAttemptsBeforeLockout: number,
      defaultAccountLockoutSeconds: number,
    },
  },
}

@Component({
  components: {
    CollapseTransition,
  },
})
export default class Setting extends AbpBase {
  name: string = 'Setting';

  rules = {};
  setting: HostSetting = {
    email:{
      defaultFromAddress:'',
      defaultFromDisplayName:'',
      smtpDomain:'',
      smtpEnableSsl:false,
      smtpHost:'',
      smtpPassword:'',
      smtpPort:25,
      smtpUseDefaultCredentials:false,
      smtpUserName:'',
    },
    periodicBackgroundWorker:0,
    security: {
      allowOneConcurrentLoginPerUser:false,
      userDefaultPasswordComplexitySettings:false,
      userLockout: {
        isEnabled: false,
        maxFailedAccessAttemptsBeforeLockout: 0,
        defaultAccountLockoutSeconds:0,
      },
    },
  };


  async save() {
    await this.$store.dispatch({
      type: 'setting/updateAllSettings',
      data: this.setting,
    });
    this.$Message.success({
      background: true,
      content: '全部设置已成功保存',
      duration: this.$config.duration,
    });
  }

  async created() {
    await this.$store.dispatch({
      type: 'setting/getAllSettings',
    });
    this.setting = this.$store.state.setting.setting;
    console.log('settings',this.setting);
  }
}
</script>

<style lang="less" scoped>
.pandx.ivu-card::v-deep>.ivu-card-body {
  height: calc(100vh - 145px);
}
</style>
